package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.NewsClassIficationDaoImpl;
import com.ysd.Dao.Impl.ProductSeriesDaoImpl;
import com.ysd.entity.NewsClassiIfication;
import com.ysd.entity.ProductSeries;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminNewsCategoryServlet extends BaseServlet {
	/**
	 * 分页查询所有产品分类
	 * localhost:8080/zhubao/adminNewsCategory?reqName=getNewsCatesByPage
	 * @author 86176
	 *
	 */
	public void getNewsCatesByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建实现类对象
		List<NewsClassiIfication> nList=nDao.grtNewsFenLeiListByPage((page-1)*size,size);//查询所有产品分类集合
		
		Gson gson= new Gson();
		String res = gson.toJson(nList);//将psList转化为json字符串
		
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
	}
	
	/**
	 * 查询所有新闻分类集合
	 * localhost:8080/zhubao/adminNewsCategory?reqName=getAllNewsCates
	 * @author 86176
	 *
	 */
	public void getAllNewsCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建实现类对象
		List<NewsClassiIfication> nList=nDao.grtNewsFenLeiList();//查询所有产品分类集合
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out = response.getWriter();
		
		Gson gson= new Gson();
		String res = gson.toJson(nList);
		
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
	}
	/**
	 * 查询所有新闻分类集合
	 * localhost:8080/zhubao/adminNewsCategory?reqName=getNewsCates
	 * @author 86176
	 *
	 */
	public void getNewsCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建实现类对象
		List<NewsClassiIfication> nList=nDao.grtNewsFenLeiList();//查询所有产品分类集合
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out = response.getWriter();
		
		Gson gson= new Gson();
		String res = gson.toJson(nList.size());
		
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
	}
	/**
	 * 删除指定新闻分类
	 * localhost:8080/zhubao/adminNewsCategory?reqName=deleteNewsCate
	 * @author 86176
	 *
	 */
		public void deleteNewsCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int nId = Integer.parseInt(request.getParameter("newsClassIficationId"));
			NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建实现类对象
			int n = nDao.deleteNewsCategoryById(nId);//调用删除方法
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			if (n>0) {//删除成功
				out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
			} else {//删除失败
				out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
			}
			
			out.flush();
			out.close();
		}
		
		
		/**
		 * 批量删除指定新闻分类  
		 * localhost:8080/zhubao/adminNewsCategory?reqName=deleteNewsCategoryByIds
		 * @author 86176
		 *
		 */
			public void deleteNewsCategoryByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				
				String  nIds = request.getParameter("newsClassIficationId");
				
				NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建实现类对象
				int n = nDao.deleteNewsCategoryByIds(nIds);//调用删除方法
				
				if (n>0) {//删除成功
					out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
				} else {//删除失败
					out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
				}
				
				out.flush();
				out.close();
			}
		/**
		 * 添加指定新闻分类
		 * localhost:8080/zhubao/adminNewsCategory?reqName=addNewsCate
		 * @author 86176
		 *
		 */
		public void addNewsCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建产品分类实现类对象
			
			String newsClassIficationName =request.getParameter("newsClassIficationName");
			if(newsClassIficationName != null && !newsClassIficationName.equals("")) {
				List<NewsClassiIfication> ncList =  nDao.grtNewsFenleiByName(newsClassIficationName);
				if(ncList.size() == 0) {
					NewsClassiIfication nc = new NewsClassiIfication(0, newsClassIficationName);
					int n =nDao.addNewsCateByName(nc);//调用添加新闻类别方法
					
					if (n>0) {//添加成功
						out.print(Result.toClient(true, "添加成功",""));//{"success":true,"message":"删除成功","remark":""}	
					} else {//添加失败
						out.print(Result.toClient(false, "添加失败",""));//{"success":false,"message":"删除失败","remark":""}	
					}
					
				}else {
					out.print(Result.toClient(false, "添加失败,系列已存在",""));
				}
			}
			
			out.flush();
			out.close();
		}
		/**
		 * 获取指定新闻分类
		 * localhost:8080/zhubao/adminNewsCategory?reqName=getNewsCateById
		 * @author 86176
		 *
		 */
		public void getNewsCateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建产品分类实现类对象
			
			int newsClassIficationId =Integer.parseInt(request.getParameter("ncId"));//接收前台传来的id
			NewsClassiIfication nsList = nDao.grtNewsFenleiById(newsClassIficationId);//调用根据id查询的方法
			
			Gson gson= new Gson();
			String res = gson.toJson(nsList);//将nsList转化为json字符串
			
			out.print(res);//响应数据
			System.out.println(res);		
			out.flush();
			out.close();
		}
		/**
		 * 编辑指定产品分类
		 * localhost:8080/zhubao/adminProductCategory?reqName=editNewsCate
		 * @author 86176
		 *
		 */
		public void editNewsCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String newsClassIficationName =request.getParameter("newsClassIficationName");
			int newsClassIficationId =Integer.parseInt(request.getParameter("newsClassIficationId"));
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			NewsClassIficationDaoImpl nDao= new NewsClassIficationDaoImpl();//创建产品分类实现类对象
			
			List<NewsClassiIfication> ncList =  nDao.grtNewsFenleiByName(newsClassIficationName);
				if(ncList.size() == 0) {
					NewsClassiIfication nc = new NewsClassiIfication(newsClassIficationId, newsClassIficationName);
					int n =nDao.editNewsCategory(nc);//调用修改新闻类别方法
					
					if (n>0) {//添加成功
						out.print(Result.toClient(true, "修改成功",""));//{"success":true,"message":"修改成功","remark":""}	
					} else {//添加失败
						out.print(Result.toClient(false, "修改失败",""));//{"success":false,"message":"修改失败","remark":""}	
					}
					
				}else {
					out.print(Result.toClient(false, "修改失败,系列已存在",""));
				}
			
			out.flush();
			out.close();
		}
		
}
